<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>axios 创建对象发送请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
    <!-- http://api.xiaohigh.com -->
    <button>王者荣耀</button>
    <button>英雄联盟</button>
    <hr>
    <!-- http://127.0.0.1:8090 -->
    <button>电影</button>
    <button>歌曲</button>

    <script>
        //调用 axios.create 创建对象  参数: 配置对象, 作为返回 axios 的默认配置
        let apiAxios = axios.create({
            method: 'get',
            baseURL: 'http://api.xiaohigh.com'
        }); // apiAxios 与 axios 使用方式『几近』是一模一样的

        let localAxios = axios.create({
            method: 'get',
            baseURL: 'http://127.0.0.1:8090'
        })

        //获取所有按钮
        let btns = document.querySelectorAll('button');
        //api.xiaohigh.com 接口调用
        btns[0].onclick = async function(){
            let {data} = await apiAxios({url: '/heros'})
            console.log(data);
        }
        btns[1].onclick = async function(){
            let {data} = await apiAxios({url: '/lol'})
            console.log(data);
        }

        //127.0.0.1:8090 接口调用
        btns[2].onclick = async function(){
            let {data} = await localAxios({url: '/movie'})
            console.log(data);
        }
        btns[3].onclick = async function(){
            let {data} = await localAxios({url: '/music'})
            console.log(data);
        }

        //使用 apiAxios 向 127.0.0.1 发送请求
        // apiAxios({
        //     method:'post',
        //     url: "http://127.0.0.1:8090/movie"
        // })

    </script>
</body>
</html>